﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Employee_CreateNewApplication : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SetDDLVacancy();
    }
    public void SetDDLVacancy()
    {
        try
        {
            if (ddlVacancy.Items.Count < 2)
            {
                Vacancy V = new Vacancy();
                DataTable tb = new DataTable();
                tb = V.TypeOfWork();
                foreach (DataRow item in tb.Rows)
                {
                    ddlVacancy.Items.Insert(Convert.ToInt32(item["JobID"].ToString()), item["JobName"].ToString());
                }
            }
        }
        catch (Exception ex)
        {
            Console.Write(ex.Message);
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        
        if (ddlVacancy.SelectedValue.Equals("Please choose Vacancy"))
        {
            lblErrorVacancy.Text = "(*)";
            lblError.Text = "Check vacancy.";
            ddlVacancy.Focus();
        }
        else if (txtFullName.Text.Equals(""))
        {
            lblErrorFullName.Text = "(*)";
            lblError.Text = "Check full name.";
            txtFullName.Focus();
        }
        else if (txtAddress.Text.Equals(""))
        {
            lblErrorAddress.Text = "(*)";
            lblError.Text = "Check Address.";
            txtAddress.Focus();
        }
        else if (txtCalendar.Text.Equals(""))
        {
            lblErrorDateOfBirth.Text = "";
            lblError.Text = "Check Date of birth.";
            txtCalendar.Focus();
        }
        else
        {
            lblError.Text = "";
            try
            {
                try
                {
                    DateTime date = new DateTime();
                    date = Convert.ToDateTime(txtCalendar.Text);
                    if (date < DateTime.Now)
                    {
                        Applicant app = new Applicant();
                        bool bol = app.InsertApplicant(ddlVacancy.SelectedValue, txtFullName.Text, txtAddress.Text, txtPhone.Text, Convert.ToDateTime(txtCalendar.Text),txtEmail.Text);
                        if (bol)
                        {
                            lblError.Text = "Successful";
                        }
                        else
                        {
                            lblError.Text = "False";
                        }
                    }
                    else
                    {
                        lblErrorDateOfBirth.Text = "(*)";
                        lblError.Text = "Date Of Birth can not lager Date now. ";
                    }
                }
                catch (Exception ex)
                {
                    lblErrorDateOfBirth.Text = "(*)";
                    lblError.Text = "Check Date of birth." + ex.Message;
                }
            }
            catch (Exception)
            {
        
            }
        }

    }
    protected void Calendar_SelectionChanged(object sender, EventArgs e)
    {
        lblErrorDateOfBirth.Text = "";
        lblError.Text = "";
        txtCalendar.Text = Calendar.SelectedDate.ToShortDateString();
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        ddlVacancy.ClearSelection();
        txtFullName.Text = "";
        txtAddress.Text = "";
        txtCalendar.Text = "";
        txtPhone.Text = "";
        txtEmail.Text = "";
    }
    public int CheckVacancy()
    {
        int result = 0;
        try
        {
            Vacancy V = new Vacancy();
            DataTable tb = new DataTable();
            tb = V.TypeOfWork();
            foreach (DataRow item in tb.Rows)
            {
                if (item["JobName"].ToString().Equals(ddlVacancy.SelectedValue))
                {
                    result = Convert.ToInt32(item["JobID"].ToString());
                }
            }
        }
        catch (Exception ex)
        {
            Console.Write(ex.Message);
        }
        return result;
    }
}
